package com.intelligentmeterreadingsys.waterlist.querywaterlist.activity;

import java.util.List;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.UserWaterVO;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;

import dbhelper.XmlNameHelper;

/**
 * 表册查询主界面
 * 
 * @author king
 * 
 */
public class QueryWaterListActivity extends ActivityGroup {
    private LinearLayout container = null;

    private RadioGroup radiogroup = null;

    private RadioButton radio2 = null;// 水表信息

    private RadioButton radio3 = null;// 数据统计

    private RadioButton radio4 = null;// 选择条件

    private RadioButton radio1 = null;// 水表列表信息

    private List<UserWaterVO> list;

    public SharedPreferenceUtil sp;

    public List<UserWaterVO> getList() {
	return list;
    }

    public void setList(List<UserWaterVO> list) {
	this.list = list;
    }

    private String waterlistid;
    private String usernumber;

    public String getWaterlistid() {
	return waterlistid;
    }

    public void setWaterlistid(String waterlistid) {
	this.waterlistid = waterlistid;
    }

    private String waternumber;
    private String copystatus;
    private String wateradds;
    private String waterproperties;
    private String waterid;
    private String username;

    public String getUsername() {
	return username;
    }

    public void setUsername(String username) {
	this.username = username;
    }

    public String getWaterid() {
	return waterid;
    }

    public void setWaterid(String waterid) {

	this.waterid = waterid;
    }

    public String getUsernumber() {
	return usernumber;
    }

    public void setUsernumber(String usernumber) {
	System.out.println(usernumber);
	this.usernumber = usernumber;
    }

    public String getWaternumber() {
	return waternumber;
    }

    public void setWaternumber(String waternumber) {
	this.waternumber = waternumber;
    }

    public String getCopystatus() {
	return copystatus;
    }

    public void setCopystatus(String copystatus) {
	System.out.println(copystatus);
	this.copystatus = copystatus;
    }

    public String getWateradds() {
	return wateradds;
    }

    public void setWateradds(String wateradds) {
	this.wateradds = wateradds;
    }

    public String getWaterproperties() {
	return waterproperties;
    }

    public void setWaterproperties(String waterproperties) {
	this.waterproperties = waterproperties;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);

	setContentView(R.layout.querywaterlist);
	getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
	sp = new SharedPreferenceUtil(this);
	inti();

    }

    /**
     * 界面切换
     * 
     * @param id
     * @param activityClass
     */
    public void launchActivity(String id, Class<?> activityClass, Bundle bundle) {
	container.removeAllViews();

	Intent intent = new Intent(QueryWaterListActivity.this, activityClass);

	if (bundle != null) {
	    intent.putExtras(bundle);
	}
	intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

	Window window = getLocalActivityManager().startActivity(id, intent);
	View view = window.getDecorView();
	container.addView(view);
    }

    /**
     * 初始化控件并添加事件
     * 
     */
    public void inti() {
	container = (LinearLayout) findViewById(R.id.container1);
	radiogroup = (RadioGroup) findViewById(R.id.querywater_main_radio);
	radio1 = (RadioButton) findViewById(R.id.waterlist_info);
	radio2 = (RadioButton) findViewById(R.id.waterlist_waterinfo);
	radio3 = (RadioButton) findViewById(R.id.waterlist_querywaterlist);
	radio4 = (RadioButton) findViewById(R.id.waterlist_querycondition);

	radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

	    @Override
	    public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (checkedId == radio2.getId()) {
		    launchActivity("WaterinfoActivity", WaterinfoActivity.class, null);
		} else if (checkedId == radio3.getId()) {

		    launchActivity("SettingStatisticsInfoActivity", SettingStatisticsInfoActivity.class, null);
		} else if (checkedId == radio1.getId()) {

		    launchActivity("ListViewActivity", ListViewActivity.class, null);
		    ListViewActivity act = (ListViewActivity) getCurrentActivity();
		    ListView listview = act.getListView();
		    list = act.getList();
		    act.flip();

		    listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			    if (list == null || list.size() < 1) {
				showMessage("没有查到水表!");
				return;
			    }
			    waterid = list.get(arg2).getWaterid();

			    radio2.setChecked(true);
			}

		    });

		} else if (checkedId == radio4.getId()) {
		    launchActivity("SetQueryConditionActivity", SetQueryConditionActivity.class, null);
		    final SetQueryConditionActivity set = (SetQueryConditionActivity) getCurrentActivity();
		    Button button = set.button1;
		    button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
			    waterlistid = set.getWaterlistid();
			    usernumber = set.edit1.getEditableText().toString();

			    waternumber = set.edit2.getEditableText().toString();
			    copystatus = set.getUsertype();
			    wateradds = set.edit4.getEditableText().toString();
			    username = set.edit3.getEditableText().toString();
			    sp.insertValue(XmlNameHelper.QUERYCOPYWATERSTATUS, copystatus);
			    sp.insertValue(XmlNameHelper.QUERYUSERNUMBER, usernumber);
			    sp.insertValue(XmlNameHelper.QUERYWATERADDS, wateradds);
			    sp.insertValue(XmlNameHelper.QUERYWATERLIST, waterlistid);
			    sp.insertValue(XmlNameHelper.QUERYWATERNUMBER, waternumber);
			    sp.insertValue(XmlNameHelper.QUERYUSERNAME, username);
			    launchActivity("ListViewActivity", ListViewActivity.class, null);
			    ListViewActivity activity = (ListViewActivity) getCurrentActivity();
			    activity.queryDater();
			    radio1.setChecked(true);

			}

		    });

		}

	    }
	});
	if (!sp.getValue(XmlNameHelper.QUERYWATERLIST).equals("请选择")) {
	    this.waterlistid = sp.getValue(XmlNameHelper.QUERYWATERLIST);

	    radio1.setChecked(true);
	} else {
	    launchActivity("SettingStatisticsInfoActivity", SettingStatisticsInfoActivity.class, null);
	    radio3.setChecked(true);
	}
    }

    /**
     * 打开子Activity的菜单事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_MENU) {
	    getCurrentActivity().openOptionsMenu();

	}
	return super.onKeyDown(keyCode, event);
    }

    // 提示消息
    public void showMessage(String message) {
	Toast toast = Toast.makeText(QueryWaterListActivity.this, message, Toast.LENGTH_SHORT);
	toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 200);
	toast.show();
    }
}
